﻿using BPMN.appilcation.IService;
using BPMN.Domid.IReposict;
using BPMN.Domid.Model.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using BPMN.Domid.Dto.UserDtos;

namespace BPMN.appilcation.Service
{
    public class UserService:ServiceBase<User>, IUserService
    {
        private readonly IUserReposict user;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        public UserService(IUserReposict user, IMapper mapper):base(user)
        {
            this.user = user;
            this.mapper = mapper;   
        }
       
       public async Task<int> AddUser(UsersDto dto)
        {
            try
            {
                 return await user.Add(mapper.Map<User>(dto));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<Return<UsersDto>> GetLong(UsersDto dto)
        {
            try
            {
                var entity = (await user.GetAll()).Where(x=>x.UserName==dto.UserName&&x.Password==dto.Password).FirstOrDefault();
                if (entity != null)
                {
                    return new Return<UsersDto>
                    {
                        Eeeor = 1,
                        Mgs = "登录成功",
                        Data = mapper.Map<UsersDto>(entity)
                    };
                }
                else
                {
                    return new Return<UsersDto>
                    {
                        Eeeor = 0,
                        Mgs = "登录失败",
                        Data = null
                    };
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

       

    }
}
